from tool.visual.popWindows.popwindows import *
import pyqtgraph as pg


class PopWindows(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, name: str):
        super().__init__()
        self.pen1 = None
        self.pen2 = None
        self.win2 = None
        self.win1 = None
        self.pen0 = None
        self.win0 = None
        self.first_pen_ref = None
        self.second_pen_ref = None
        self.third_pen_ref = None
        self.first_pen_pre = None
        self.second_pen_pre = None
        self.third_pen_pre = None
        self.setupUi(self, name)
        self.ref_pen = pg.mkPen(color=(34, 34, 34), width=2)
        self.pre_pen = pg.mkPen(color=(254, 73, 38), width=2)
        self.vertical_layout = None


    def init_windows(self, first: str, second: str, third: str):
        self.vertical_layout = QtWidgets.QVBoxLayout(self.widget)
        self.vertical_layout.setSpacing(0)

        # self.win0 = MyMplCanvas(self.widget, width=5, height=4, dpi=100)
        # self.win0.axes.set_title(first)
        # self.vertical_layout.addWidget(self.win0)
        #
        # self.win1 = MyMplCanvas(self.widget, width=5, height=4, dpi=100)
        # self.win1.axes.set_title(second)
        # self.vertical_layout.addWidget(self.win1)
        #
        # self.win2 = MyMplCanvas(self.widget, width=5, height=4, dpi=100)
        # self.win2.axes.set_title(third)
        # self.vertical_layout.addWidget(self.win2)

        self.win0 = pg.GraphicsLayoutWidget()
        self.win0.setBackground((255, 255, 255))  # 定义背景颜色
        self.win0.enableMouse(False)
        self.vertical_layout.addWidget(self.win0)  # 放入垂直布局
        self.pen0 = self.win0.addPlot(
            title="<span style='font-size:12px;color:black'>" + first + "</span>")
        self.pen0.setLabel('bottom', "<span style='font-size:10px;color:black'>X(N)</span>")  # 设置x轴标注
        self.pen0.setLabel('left', "<span style='font-size:10px;color:black'>Y(m)</span>")  # 设置y轴标注

        self.win1 = pg.GraphicsLayoutWidget()  # 创建图形布局小部件
        self.win1.setBackground((255, 255, 255))  # 定义背景颜色
        self.vertical_layout.addWidget(self.win1)  # 放入垂直布局
        self.win1.enableMouse(False)
        self.pen1 = self.win1.addPlot(
            title="<span style='font-size:12px;color:black'>" + second + "</span>")
        self.pen1.setLabel('bottom', "<span style='font-size:10px;color:black'>X(N)</span>")  # 设置x轴标注
        self.pen1.setLabel('left', "<span style='font-size:10px;color:black'>Y(m/s)</span>")  # 设置y轴标注

        self.win2 = pg.GraphicsLayoutWidget()
        self.win2.setBackground((255, 255, 255))
        self.vertical_layout.addWidget(self.win2)
        self.win2.enableMouse(False)
        self.pen2 = self.win2.addPlot(
            title="<span style='font-size:12px;color:black'>" + third + "</span>")
        self.pen2.setLabel('bottom', "<span style='font-size:10px;color:black'>X(N)</span>")  # 设置x轴标注
        self.pen2.setLabel('left', "<span style='font-size:10px;color:black'>Y(m/s^2)</span>")  # 设置y轴标注

    def winaddLegend(self):
        if self.pen0.legend is not None:
            self.pen0.legend.clear()
            self.pen1.legend.clear()
            self.pen2.legend.clear()
        legend = self.pen0.addLegend(itemPos=(1, 0), parentPos=(1, 0), offset=(-1, 1))
        legend.addItem(self.first_pen_ref, 'Reference')
        legend.addItem(self.first_pen_pre, 'Prediction')
        legend = self.pen1.addLegend(itemPos=(1, 0), parentPos=(1, 0), offset=(-1, 1))
        legend.addItem(self.second_pen_ref, 'Reference')
        legend.addItem(self.second_pen_pre, 'Prediction')
        legend = self.pen2.addLegend(itemPos=(1, 0), parentPos=(1, 0), offset=(-1, 1))
        legend.addItem(self.third_pen_ref, 'Reference')
        legend.addItem(self.third_pen_pre, 'Prediction')

    def update_data_ref(self, data1, data2, data3):
        if self.first_pen_ref is not None:
            self.pen0.removeItem(self.first_pen_ref)
        if self.second_pen_ref is not None:
            self.pen1.removeItem(self.second_pen_ref)
        if self.third_pen_ref is not None:
            self.pen2.removeItem(self.third_pen_ref)
        self.first_pen_ref = self.pen0.plot(data1, pen=self.ref_pen, name='Reference')
        self.second_pen_ref = self.pen1.plot(data2, pen=self.ref_pen, name='Reference')
        self.third_pen_ref = self.pen2.plot(data3, pen=self.ref_pen, name='Reference')
        self.winaddLegend()

    def update_data_pre(self, data1, data2, data3):
        if self.first_pen_pre is not None:
            self.pen0.removeItem(self.first_pen_pre)
        if self.second_pen_pre is not None:
            self.pen1.removeItem(self.second_pen_pre)
        if self.third_pen_pre is not None:
            self.pen2.removeItem(self.third_pen_pre)
        self.first_pen_pre = self.pen0.plot(data1, pen=self.pre_pen, name='Prediction')
        self.second_pen_pre = self.pen1.plot(data2, pen=self.pre_pen, name='Prediction')
        self.third_pen_pre = self.pen2.plot(data3, pen=self.pre_pen, name='Prediction')
        self.winaddLegend()

